import {
  formatTime
} from '../../../utils/util'
import {
  toast
} from '../../../utils/Tools'
//引入
import {
  getTable,
  getTableDown,
  getTableEdit
} from '../../../network/finance'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 
    date: "选择时间",
    // 数据渲染
    tableInfo: [],
    // 搜索框内容
    payCause: '',
    // 下载链接
    TableDown: '',
    // 下拉选择框
    options: [
      // {
      //   // city_id: '1',
      //   city_name: '通过审核'
      // }, {
      //   // city_id: '1',
      //   city_name: '拒绝审核'
      // }
    ],
    defaultOption: {
      // id:-1,
      name: "请选择付款方式"
    },

    selected: {},
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getTable()
    // this.
    let date = formatTime(new Date())
    this.setData({
      date
    })
  },
 
  // 下拉选择框相关
  change(e) {
    console.log("下拉", e);
    let {
      id,
      name
    } = e.target.dataset
    console.log("idname", id, name);
    this.setData({
      selected: {
        ...e.detail
      }
    })
    this._getTableEdit(id, name)
  },
  // 修改付款方式和公共付款方式
  // mobileadmin/get_table_edit
  async _getTableEdit(id, name) {
    let data = {
      name,
      date: this.data.date + "-01 00:00:00",
      value: this.data.selected.name,
      id
    }
    console.log("value", this.data.selected);
    const res = await getTableEdit(data)
    console.log("修改返回", res);
  },
  // 应收账款汇总表下载
  async _getTableDown() {
    let data = {
      start_date: this.data.date === "选择时间" ? '' : this.data.date
    }
    const res = await getTableDown(data)
    toast("正在打开")
    if (res.code === 200) {

      this.setData({
        TableDown: res.data
      })

    }
    wx.downloadFile({
      url: `http://master.gddzxcx.com/${this.data.TableDown}`,
      success: function (res) {
        var filePath = res.tempFilePath;
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
            toast("打开文档成功", "success")
          }
        })
      }
    })
  },
  // 下载按钮
  load() {

  },
  // 改变时间时触发
  bindDateChange: function (e) {
    this.setData({
      //将当前选择时间传入date变量中
      date: e.detail.value
    })

  },
  // 监听输入框 //
  bindText(e) {
    this.setData({
      // 获取输入框长度
      t_length: e.detail.value.length,
      //获取输入框内容
      payCause: e.detail.value
    })
    if (this.data.payCause.length === 0) {
      this._getTable()
    }
  },
 
  /**
   * 页面触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("刷新");
    this.setData({
      page: this.data.page + 1
    })
    console.log("页面值", this.data.page);
    this._getTable()
    // this.pageScrollToBottom()
  },

  // 页面最底部
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom
      })
    }).exec()
  },
  // 网络请求 //
  async _getTable(e) {
    // console.log("e", e);
    let data = {
      customer_name: this.data.payCause,
      start_date: this.data.date == "选择时间" ? '' : this.data.date,
      page: this.data.page
    }
    // this.data.page
    const res = await getTable(data)
    if (res.code === 200) {
      var tableTem = this.data.tableInfo
      this.setData({
        tableInfo: tableTem.concat(res.data.supplyer.data)
      })
      var arr = []
      for (var key in res.data.basic_monthly) {
        let obj = {}
        obj.city_id = 1
        obj.city_name = key
        arr.push(obj)
        this.setData({
          options: arr
        })
      }
    }
  }
})